﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Web;
using System.Reflection;
using System.ComponentModel;

namespace SDT.Libraries.Utilities
{
    public static class FrameworkExtension
    {
        /// <summary>
        /// Display description of the enum type.
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static string ToDescription(this Enum en)
        {
            Type type = en.GetType();
            MemberInfo[] memInfo = type.GetMember(en.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }

            return en.ToString();
        }

        /// <summary>
        /// Convert a string to capitalize first character in each word.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ToTextCapitalized(this string source)
        {
            if (string.IsNullOrEmpty(source))
                return string.Empty;

            return String.Join(" ", source.Split(new string[] { " " },
                StringSplitOptions.RemoveEmptyEntries).Select(temp => temp.ToUpperFirstLetter()).ToArray());
        }

        /// <summary>
        /// Convert the first character to upper case.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ToUpperFirstLetter(this string source)
        {
            if (string.IsNullOrEmpty(source))
                return string.Empty;

            char[] letters = source.ToCharArray();
            letters[0] = char.ToUpper(letters[0]);
            return new string(letters);

        }

        //public static TAttribute GetAttributeOf<TAttribute>(this PropertyInfo propertyInfo)
        //{
        //    object[] attributes = propertyInfo.GetCustomAttributes(typeof(TAttribute), true);
        //    if (attributes.Length == 0)
        //        return default(TAttribute);

        //    return (TAttribute)attributes[0];
        //}

        //public static bool IsPrimaryKey(this PropertyInfo propertyInfo)
        //{
        //    var columnAttribute = propertyInfo.GetAttributeOf<ColumnAttribute>();
        //    if (columnAttribute == null) 
        //        return false;
            
        //    return columnAttribute.IsPrimaryKey;
        //}

        //public static PropertyInfo GetPrimaryKey(this Type entityType)
        //{
        //    foreach (PropertyInfo property in entityType.GetProperties())
        //    {
        //        if (property.IsPrimaryKey())
        //        {
        //            return property;
        //        }
        //    }

        //    throw new ApplicationException(string.Format("No primary key defined for type {0}", entityType.Name));
        //}
    }
}
